DepthToSpace

将输入张量的深度通道按 block_size 重排到空间维(Depth → Space)。 输入/输出均为 NHWC 格式:[batch, height, width, channel]。 要求输入通道数 \(C\) 能被 \(B^{2}\) 整除,其中 \(B\)block_size

设输入形状为 \([N, H, W, C]\),则输出形状为:

\[\begin{split}\begin{aligned} N_{out} &= N \\ H_{out} &= H \cdot B \\ W_{out} &= W \cdot B \\ C_{out} &= C / B^{2} \end{aligned}\end{split}\]

mode 约定

  • mode = 0:DCR(Depth-Column-Row)

  • mode != 0:CRD(Column-Row-Depth)

当前测试与常用路径为 DCR。对输入坐标 \((n, y, x, c)\),在 DCR 模式下:

\[\begin{split}\begin{aligned} y_{out} &= y \cdot B + \left\lfloor (c / C_{out}) / B \right\rfloor \\ x_{out} &= x \cdot B + (c / C_{out}) \bmod B \\ c_{out} &= c \bmod C_{out} \end{aligned}\end{split}\]

\(O[n, y_{out}, x_{out}, c_{out}] = I[n, y, x, c]\)

输入:
  • input - 输入数据地址

  • in_shape - 输入形状,格式为 [batch, height, width, channel]

  • block_size - 分块因子 \(B\)

  • mode - 重排模式(0 为 DCR,非 0 为 CRD)

  • core_mask - 核掩码(仅共享存储版本使用)

输出:
  • output - 输出数据地址

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 int8, int16, int32, fp32, fp64, cplx64, cplx128

  • MT7004 支持 int16, int32, fp16, fp32, cplx64

共享存储版本:

void i8_depth_to_space_s(int8_t *input, int8_t *output, int *in_shape, int block_size, int mode, int core_mask)
void i16_depth_to_space_s(int16_t *input, int16_t *output, int *in_shape, int block_size, int mode, int core_mask)
void i32_depth_to_space_s(int *input, int *output, int *in_shape, int block_size, int mode, int core_mask)
void hp_depth_to_space_s(float16 *input, float16 *output, int *in_shape, int block_size, int mode, int core_mask)
void fp_depth_to_space_s(float *input, float *output, int *in_shape, int block_size, int mode, int core_mask)
void dp_depth_to_space_s(double *input, double *output, int *in_shape, int block_size, int mode, int core_mask)
void c64_depth_to_space_s(float *input, float *output, int *in_shape, int block_size, int mode, int core_mask)
void c128_depth_to_space_s(double *input, double *output, int *in_shape, int block_size, int mode, int core_mask)

C调用示例:

 1// MT7004 示例(共享存储多核,DDR 地址)
 2void TestDepthToSpaceSMCFp32(int core_mask) {
 3    int core_id = get_core_id();
 4    int logic_core_id = GetLogicCoreId(core_mask, core_id);
 5    int core_num = GetCoreNum(core_mask);
 6    float *input = (float *)0x81000000;
 7    float *output = (float *)0x82000000;
 8    int in_shape[4] = {1, 8, 8, 4};
 9    int block_size = 2;
10    int mode = 0; // DCR
11    sys_bar(0, core_num);
12    fp_depth_to_space_s(input, output, in_shape, block_size, mode, core_mask);
13}
14
15void main() {
16    int core_mask = 0b1111;
17    TestDepthToSpaceSMCFp32(core_mask);
18}

私有存储版本:

void i8_depth_to_space_p(int8_t *input, int8_t *output, int *in_shape, int block_size, int mode)
void i16_depth_to_space_p(int16_t *input, int16_t *output, int *in_shape, int block_size, int mode)
void i32_depth_to_space_p(int *input, int *output, int *in_shape, int block_size, int mode)
void hp_depth_to_space_p(float16 *input, float16 *output, int *in_shape, int block_size, int mode)
void fp_depth_to_space_p(float *input, float *output, int *in_shape, int block_size, int mode)
void dp_depth_to_space_p(double *input, double *output, int *in_shape, int block_size, int mode)
void c64_depth_to_space_p(float *input, float *output, int *in_shape, int block_size, int mode)
void c128_depth_to_space_p(double *input, double *output, int *in_shape, int block_size, int mode)

C调用示例:

 1// MT7004 示例(私有存储单核,AM 地址)
 2void TestDepthToSpaceAMFp32(void) {
 3    float *input = (float *)0x10000000;
 4    float *output = (float *)0x10010000;
 5    int in_shape[4] = {1, 8, 8, 4};
 6    int block_size = 2;
 7    int mode = 0; // DCR
 8    fp_depth_to_space_p(input, output, in_shape, block_size, mode);
 9}
10
11void main() {
12    TestDepthToSpaceAMFp32();
13}